#include "BDThread.h"

unsigned int __stdcall threadFunction(void* pThread, void* data)
{
	BDThread* p = (BDThread*)pThread;
	return (unsigned int)p->Run(data);
}

BDThread::BDThread()
	: m_Thread()
	, m_bStarted(false)
	, m_bDetached(false)
{

}

BDThread::BDThread(std::function<void*(void*)> function)
	: m_Thread()
	, m_bStarted(false)
	, m_bDetached(false)
	, m_Function(function)
{

}

BDThread::~BDThread()
{

}

void* BDThread::Run(void* data)
{
	return m_Function(data);
}

int BDThread::Start(void* data)
{
	if (!m_bStarted) {
		m_Thread = std::thread(threadFunction, this, data);
		m_bStarted = true;
	}
	return m_bStarted;
}

void BDThread::Wait()
{
	DWORD status = (DWORD)NULL;
	if (m_bStarted && !m_bDetached) {
		m_Thread.join();
		m_bDetached = true;
	}
}

void BDThread::Detach()
{
	if (m_bStarted && !m_bDetached) {
		m_Thread.detach();
	}
	m_bDetached = true;
}

void BDThread::Stop()
{
	
}

void BDThread::Sleep(int ms)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}